#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
  * @brief  Get file-stream's length.
  * @Notes  None.
  * @param  fp, file pointer.
  * @retval stream's length which in a file.
  */
long fstr_len(FILE* fp)
{
	long stream_len; 
	fseek(fp, 0, SEEK_END); 
	stream_len = ftell(fp); 
	fseek(fp, 0, SEEK_SET);
	//printf("length=%d\n", stream_len);
	return stream_len;
}

/**
  * @brief  Allocate memory space for file-stream.
  * @Notes  None.
  * @param  fp, file pointer.
  * @param  stream, memory buffer for stream.
  * @retval stream's length which in a file.
  */
long fstr_alloc(FILE* fp, char** stream)
{
	long chars = fstr_len(fp);
	long rdsz;
	if(chars == 0)
	{
		return 0;
	}
	else{
		*stream = (char*)malloc((chars)*sizeof(char));
		rdsz = fread(*stream, 1, chars, fp);
		//printf("fstrbuf:%s\n", fstrbuf);
		return rdsz;
	}
}

/**
  * @brief  Release memory space of file-stream.
  * @Notes  None.
  * @param  fstrbuf, memory buffer for stream.
  * @retval None.
  */
void fstr_free(char* fstrbuf)
{
	if(fstrbuf != NULL)
		free(fstrbuf);
}
